<?php

// common.php - 小蛋糕聊天室公共文件 版权所有 © 2025

session_start();

// 文件路径

define('USER_FILE', 'data/yh.txt');

define('CHAT_FILE', 'data/data.txt');

// 管理员账号

$admin_accounts = [

    'admin' => 'admin',

    'admin2' => 'admin2',

    'admin3' => 'admin3'

];

// 创建数据目录

if (!file_exists('data')) {

    mkdir('data', 0777, true);

}

// 初始化文件

if (!file_exists(USER_FILE)) {

    file_put_contents(USER_FILE, "");

}

if (!file_exists(CHAT_FILE)) {

    file_put_contents(CHAT_FILE, "");

}

// 获取用户列表

function getUsers() {

    if (!file_exists(USER_FILE)) {

        return [];

    }

    

    $content = file_get_contents(USER_FILE);

    if (empty($content)) {

        return [];

    }

    

    $users = json_decode($content, true) ?: [];

    

    // 清理过期的禁言

    $currentTime = time();

    foreach ($users as $username => &$userData) {

        if (isset($userData['muted_until']) && $userData['muted_until'] > 0 && $userData['muted_until'] < $currentTime) {

            $userData['muted'] = false;

            $userData['muted_until'] = 0;

            $userData['mute_reason'] = '';

        }

    }

    

    return $users;

}

// 保存用户数据

function saveUsers($users) {

    return file_put_contents(USER_FILE, json_encode($users)) !== false;

}

// 保存消息

function saveMessage($messageData) {

    $messages = getMessages();

    $messages[] = $messageData;

    

    // 只保留最近200条消息

    if (count($messages) > 200) {

        $messages = array_slice($messages, -200);

    }

    

    return file_put_contents(CHAT_FILE, json_encode($messages)) !== false;

}

// 获取消息

function getMessages() {

    if (!file_exists(CHAT_FILE)) {

        return [];

    }

    

    $content = file_get_contents(CHAT_FILE);

    if (empty($content)) {

        return [];

    }

    

    return json_decode($content, true) ?: [];

}

// 检查是否登录

function checkLogin() {

    if (!isset($_SESSION['username'])) {

        header("Location: login.php");

        exit;

    }

}

// 显示消息提示

function displayMessages() {

    if (isset($_SESSION['error'])) {

        echo '<div class="message error">' . $_SESSION['error'] . '</div>';

        unset($_SESSION['error']);

    }

    if (isset($_SESSION['success'])) {

        echo '<div class="message success">' . $_SESSION['success'] . '</div>';

        unset($_SESSION['success']);

    }

}

// 检查用户是否被禁言

function isUserMuted($username) {

    $users = getUsers();

    if (!isset($users[$username])) {

        return false;

    }

    

    $userData = $users[$username];

    

    // 检查永久禁言

    if ($userData['muted'] && $userData['muted_until'] === 0) {

        return true;

    }

    

    // 检查临时禁言

    if ($userData['muted'] && $userData['muted_until'] > time()) {

        return true;

    }

    

    // 禁言已过期，解除禁言

    if ($userData['muted'] && $userData['muted_until'] > 0 && $userData['muted_until'] <= time()) {

        $users[$username]['muted'] = false;

        $users[$username]['muted_until'] = 0;

        $users[$username]['mute_reason'] = '';

        saveUsers($users);

        return false;

    }

    

    return false;

}

// 获取禁言剩余时间

function getMuteTimeLeft($username) {

    $users = getUsers();

    if (!isset($users[$username]) || !$users[$username]['muted']) {

        return 0;

    }

    

    $mutedUntil = $users[$username]['muted_until'];

    if ($mutedUntil === 0) {

        return '永久';

    }

    

    $timeLeft = $mutedUntil - time();

    if ($timeLeft <= 0) {

        return 0;

    }

    

    $minutes = ceil($timeLeft / 60);

    $hours = floor($minutes / 60);

    $minutes = $minutes % 60;

    

    if ($hours > 0) {

        return "{$hours}小时{$minutes}分钟";

    } else {

        return "{$minutes}分钟";

    }

}

?>